/*
 * Copyright (C) 2015 kmy-framework-base Project
 *               Author: Administrator
 *               Date: 2015年11月4日
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zmq.ha.client.resource;

import java.util.concurrent.ExecutorService;
import com.rabbitmq.client.Address;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 
 * Name: ConnectionOption.java
 * ProjectName: [kmhc-framework-base]
 * Package: [com.kmhc.framework.base.rabbitmq.recover.resource.ConnectionOption.java]
 * Description: TODO  
 * 
 * @since JDK1.7
 * @see
 *
 * @author: Chris
 * @date: 2016年3月11日 下午3:55:47
 *
 * Update-User: @author
 * Update-Time:
 * Update-Remark:
 * 
 * Check-User:
 * Check-Time:
 * Check-Remark:
 * 
 * Company: kmhc
 * Copyright: kmhc
 */
public class ConnectionOption {
	
	private ConnectionFactory connectionFactory;
	private String[] hostName;
	private String port;
	private String userName;
	private String passwork;
	private ExecutorService consumerPool;

	private Address[] addresses;
	
	public ConnectionOption() {
		connectionFactory = new ConnectionFactory();
	}

	public ConnectionOption(ConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}

	public ConnectionOption withHostName(String hostName) {
		this.hostName = new String[]{hostName};
		return this;
	}

	public ConnectionOption withPort(String port) {
		this.port = port;
		return this;
	}

	public ConnectionOption withUserName(String userName) {
		this.userName = userName;
		return this;
	}

	public ConnectionOption withPassowrd(String password) {

		this.passwork = password;
		return this;
	}

	public ConnectionOption withComsumerThreadPool(ExecutorService threadPool) {
		this.consumerPool = threadPool;
		return this;
	}

	public ConnectionFactory getConnectionFactory() {
		return connectionFactory;
	}

	public void setConnectionFactory(ConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}

	public ExecutorService getConsumerPool() {
		return consumerPool;
	}

	public void setConsumerPool(ExecutorService consumerPool) {
		this.consumerPool = consumerPool;
	}


	public String[] getHostName() {
		return hostName;
	}

	public void setHostName(String[] hostName) {
		this.hostName = hostName;
	}

	public void setAddresses(Address[] addresses) {
		this.addresses = addresses;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPasswork() {
		return passwork;
	}

	public void setPasswork(String passwork) {
		this.passwork = passwork;
	}
	
	public Address[] getAddresses() {
		if (addresses != null)
			return addresses;

		if (hostName != null) {
			addresses = new Address[hostName.length];
			for (int i = 0; i < hostName.length; i++)
				addresses[i] = new Address(hostName[i], connectionFactory.getPort());
			return addresses;
		}

		Address address = connectionFactory == null ? new Address("localhost", -1)
				: new Address(connectionFactory.getHost(), connectionFactory.getPort());
		return new Address[] { address };
	}

}
